package cn.edu.swu.auth;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.junit.runner.Request;

import java.io.IOException;
import java.util.Objects;

//@WebServlet(urlPatterns = "/picture/*")   老子每次用这个都出问题!tmd!
public class AuthFilter implements Filter {

    public final static String LOGIN_TAG="LOGIN_TAG";
    public final static String LOGIN_TAG_SUCCESS="LOGIN_TAG_SUCCESS";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpSession session = ((HttpServletRequest) request).getSession(true);
        Object tag = session.getAttribute(LOGIN_TAG);
        //判断是否为空
        if(tag == null || !tag.equals(LOGIN_TAG_SUCCESS)){
            ((HttpServletResponse) response).sendRedirect("/bookstore-demo/admin.html");
        }else{      //存在且不为空就放行
            chain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
